package com.dao.connleak;

import java.sql.Connection;
import java.sql.SQLException;

import org.apache.commons.dbcp.BasicDataSource;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service("jdbcUserService")
public class JdbcUserService {
	
	private static final Logger logger = Logger.getLogger(JdbcUserService.class);
	private static String configPath = "applicationContext.xml";
	private static ApplicationContext applicationContext = new ClassPathXmlApplicationContext(configPath);;
	
	@Autowired
	private JdbcTemplate jdbcTemplate;
	
	@Transactional
	public void logon(String userName)
	{
		Connection conn = null;
		try {
//			conn = jdbcTemplate.getDataSource().getConnection();
			conn = DataSourceUtils.getConnection( jdbcTemplate.getDataSource());
			String sql = "UPDATE t_user set LAST_LOGON_TIME=? WHERE USER_NAME = ?";
			jdbcTemplate.update(sql,System.currentTimeMillis(),userName);
			
			Thread.sleep(1000);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public static void asynchrLogon(JdbcUserService userService, String userName)
	{
		UserServiceRunner runner = new UserServiceRunner(userService, userName);
				runner.start();
	}
	
	private static class UserServiceRunner extends Thread
	{
		private JdbcUserService userService;
		private String userName;
		public UserServiceRunner(JdbcUserService userService,String userName)
		{
			this.userService = userService;
			this.userName = userName;
		}
		
		public void run()
		{
			userService.logon(userName);
		}
	}
	
	public static void sleep(long time)
	{
		try {
			Thread.sleep(time);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public static void reportConn(BasicDataSource basicDataSource)
	{
		logger.debug("连接数【active：idle】- 【" + basicDataSource.getNumActive()+ ":" + basicDataSource.getNumIdle() + "】");
	}
	
	public static void main(String[] args) {
		JdbcUserService jdbcUserService = (JdbcUserService) applicationContext.getBean("jdbcUserService");
		BasicDataSource basicDataSource = (BasicDataSource) applicationContext.getBean("dataSource1");
		JdbcUserService.reportConn(basicDataSource);
		
		JdbcUserService.asynchrLogon(jdbcUserService, "tom");
		JdbcUserService.sleep(500);
		JdbcUserService.reportConn(basicDataSource);
		
		JdbcUserService.sleep(2000);
		JdbcUserService.reportConn(basicDataSource);
		
		JdbcUserService.asynchrLogon(jdbcUserService, "John");
		JdbcUserService.sleep(500);
		JdbcUserService.reportConn(basicDataSource);
		
		JdbcUserService.sleep(2000);
		JdbcUserService.reportConn(basicDataSource);
	}
}
